/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmPreStrainsDialog dgl = new ElmPreStrainsDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmPreStrainsDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private FemModel fmodel;
	
	private String anlType;
	
	private DecimalField psDx1Field;
	private DecimalField psDy1Field;
	private DecimalField psDz1Field;
	private DecimalField psRx1Field;
	private DecimalField psRy1Field;
	private DecimalField psRz1Field;
	private DecimalField psDx2Field;
	private DecimalField psDy2Field;
	private DecimalField psDz2Field;
	private DecimalField psRx2Field;
	private DecimalField psRy2Field;
	private DecimalField psRz2Field;
	public double psDx1;
	public double psDy1;
	public double psDz1;
	public double psRx1;
	public double psRy1;
	public double psRz1;
	public double psDx2;
	public double psDy2;
	public double psDz2;
	public double psRx2;
	public double psRy2;
	public double psRz2;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmPreStrainsDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		setUpFormat();
		setUpFields();
		
//		Sets up the pre-strains panel
		JPanel preStrainsPane = new JPanel();
		preStrainsPane.setPreferredSize(new Dimension(360,280));
		preStrainsPane.setLayout(new BoxLayout(preStrainsPane, BoxLayout.Y_AXIS));
		
		JPanel psPicPane = new JPanel();
		psPicPane.setMaximumSize(new Dimension(500,100));
		psPicPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/PreStrains100.gif")) ));
		preStrainsPane.add(psPicPane);
		
		JPanel ps1Pane = new JPanel();
		ps1Pane.setLayout(new GridLayout(2,3,2,2));
		ps1Pane.setMaximumSize(new Dimension(500,90));
		ps1Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
			
		JPanel psD1xPane = new JPanel();
		JPanel psD1yPane = new JPanel();
		JPanel psD1zPane = new JPanel();
		psD1xPane.add(new JLabel("Dx:"));
		psD1xPane.add(psDx1Field);
		psD1yPane.add(new JLabel("Dy:"));
		psD1yPane.add(psDy1Field);
		psD1zPane.add(new JLabel("Dz:"));
		psD1zPane.add(psDz1Field);
		ps1Pane.add(psD1xPane);
		ps1Pane.add(psD1yPane);
		ps1Pane.add(psD1zPane);
		
		JPanel psR1xPane = new JPanel();
		JPanel psR1yPane = new JPanel();
		JPanel psR1zPane = new JPanel();
		psR1xPane.add(new JLabel("Rx:"));
		psR1xPane.add(psRx1Field);
		psR1yPane.add(new JLabel("Ry:"));
		psR1yPane.add(psRy1Field);
		psR1zPane.add(new JLabel("Rz:"));
		psR1zPane.add(psRz1Field);
		ps1Pane.add(psR1xPane);
		ps1Pane.add(psR1yPane);
		ps1Pane.add(psR1zPane);
		
		JPanel ps2Pane = new JPanel();
		ps2Pane.setLayout(new GridLayout(2,3,2,2));
		ps2Pane.setMaximumSize(new Dimension(500,90));
		ps2Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel psD2xPane = new JPanel();
		JPanel psD2yPane = new JPanel();
		JPanel psD2zPane = new JPanel();
		psD2xPane.add(new JLabel("Dx:"));
		psD2xPane.add(psDx2Field);
		psD2yPane.add(new JLabel("Dy:"));
		psD2yPane.add(psDy2Field);
		psD2zPane.add(new JLabel("Dz:"));
		psD2zPane.add(psDz2Field);
		ps2Pane.add(psD2xPane);
		ps2Pane.add(psD2yPane);
		ps2Pane.add(psD2zPane);
		
		JPanel psR2xPane = new JPanel();
		JPanel psR2yPane = new JPanel();
		JPanel psR2zPane = new JPanel();
		psR2xPane.add(new JLabel("Rx:"));
		psR2xPane.add(psRx2Field);
		psR2yPane.add(new JLabel("Ry:"));
		psR2yPane.add(psRy2Field);
		psR2zPane.add(new JLabel("Rz:"));
		psR2zPane.add(psRz2Field);
		ps2Pane.add(psR2xPane);
		ps2Pane.add(psR2yPane);
		ps2Pane.add(psR2zPane);
		
		preStrainsPane.add(ps1Pane);
		preStrainsPane.add(ps2Pane);
		
		setPane(preStrainsPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dElmElaConDialog06"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			
			psDx1 = psDx1Field.getValue();
			psDy1 = psDy1Field.getValue();
			psDz1 = psDz1Field.getValue();
			psRx1 = psRx1Field.getValue();
			psRy1 = psRy1Field.getValue();
			psRz1 = psRz1Field.getValue();
			psDx2 = psDx2Field.getValue();
			psDy2 = psDy2Field.getValue();
			psDz2 = psDz2Field.getValue();
			psRx2 = psRx2Field.getValue();
			psRy2 = psRy2Field.getValue();
			psRz2 = psRz2Field.getValue();
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting pre-strains fields
			psDx1Field = new DecimalField(elm.getElmPreDisplacement(0), 7, numberFormat);
			psDy1Field = new DecimalField(elm.getElmPreDisplacement(1), 7, numberFormat);
			psDz1Field = new DecimalField(elm.getElmPreDisplacement(2), 7, numberFormat);
			psRx1Field = new DecimalField(elm.getElmPreDisplacement(3), 7, numberFormat);
			psRy1Field = new DecimalField(elm.getElmPreDisplacement(4), 7, numberFormat);
			psRz1Field = new DecimalField(elm.getElmPreDisplacement(5), 7, numberFormat);
			psDx2Field = new DecimalField(elm.getElmPreDisplacement(6), 7, numberFormat);
			psDy2Field = new DecimalField(elm.getElmPreDisplacement(7), 7, numberFormat);
			psDz2Field = new DecimalField(elm.getElmPreDisplacement(8), 7, numberFormat);
			psRx2Field = new DecimalField(elm.getElmPreDisplacement(9), 7, numberFormat);
			psRy2Field = new DecimalField(elm.getElmPreDisplacement(10), 7, numberFormat);
			psRz2Field = new DecimalField(elm.getElmPreDisplacement(11), 7, numberFormat);
			
		} else {
			
			//Setting pre-strains fields
			psDx1Field = new DecimalField(0, 7, numberFormat);
			psDy1Field = new DecimalField(0, 7, numberFormat);
			psDz1Field = new DecimalField(0, 7, numberFormat);
			psRx1Field = new DecimalField(0, 7, numberFormat);
			psRy1Field = new DecimalField(0, 7, numberFormat);
			psRz1Field = new DecimalField(0, 7, numberFormat);
			psDx2Field = new DecimalField(0, 7, numberFormat);
			psDy2Field = new DecimalField(0, 7, numberFormat);
			psDz2Field = new DecimalField(0, 7, numberFormat);
			psRx2Field = new DecimalField(0, 7, numberFormat);
			psRy2Field = new DecimalField(0, 7, numberFormat);
			psRz2Field = new DecimalField(0, 7, numberFormat);
			
		}
		
		//Enabling or disabling fields according to analysis model type
		if (this.parent.getController().getSelection().size() == 1)
			anlType = ((FrameElement)this.parent.getController().getSelection().get(0)).getAnalysisModel().getType();
		else anlType = fmodel.getGlobalAnalysisModel().getType();
		
		if (anlType.equals("Beam")) {
			
			psDx1Field.setEnabled(false);
			psDx2Field.setEnabled(false);
			psDy1Field.setEnabled(true);
			psDy2Field.setEnabled(true);
			psDz1Field.setEnabled(false);
			psDz2Field.setEnabled(false);
			psRx1Field.setEnabled(false);
			psRx2Field.setEnabled(false);
			psRy1Field.setEnabled(false);
			psRy2Field.setEnabled(false);
			psRz1Field.setEnabled(true);
			psRz2Field.setEnabled(true);
			
		} else if (anlType.equals("PlaneFrame")) {
			
			psDx1Field.setEnabled(true);
			psDx2Field.setEnabled(true);
			psDy1Field.setEnabled(true);
			psDy2Field.setEnabled(true);
			psDz1Field.setEnabled(false);
			psDz2Field.setEnabled(false);
			psRx1Field.setEnabled(false);
			psRx2Field.setEnabled(false);
			psRy1Field.setEnabled(false);
			psRy2Field.setEnabled(false);
			psRz1Field.setEnabled(true);
			psRz2Field.setEnabled(true);
			
		} else if (anlType.equals("PlaneTruss")) {
			
			psDx1Field.setEnabled(true);
			psDx2Field.setEnabled(true);
			psDy1Field.setEnabled(true);
			psDy2Field.setEnabled(true);
			psDz1Field.setEnabled(false);
			psDz2Field.setEnabled(false);
			psRx1Field.setEnabled(false);
			psRx2Field.setEnabled(false);
			psRy1Field.setEnabled(false);
			psRy2Field.setEnabled(false);
			psRz1Field.setEnabled(false);
			psRz2Field.setEnabled(false);
			
		} else if (anlType.equals("Grid")) {
			
			psDx1Field.setEnabled(false);
			psDx2Field.setEnabled(false);
			psDy1Field.setEnabled(true);
			psDy2Field.setEnabled(true);
			psDz1Field.setEnabled(false);
			psDz2Field.setEnabled(false);
			psRx1Field.setEnabled(true);
			psRx2Field.setEnabled(true);
			psRy1Field.setEnabled(false);
			psRy2Field.setEnabled(false);
			psRz1Field.setEnabled(true);
			psRz2Field.setEnabled(true);
		
		}
		
	}
	
//*****************************************************************************
}

